import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by dell on 2017/8/9.
 */

public class PostTomcat {
    public void copy(String net, String key, String cityName) throws Exception {
        try {
            URL url = new URL(net);
            URLConnection uc = url.openConnection();
            HttpURLConnection hu = (HttpURLConnection) uc;
            hu.setDoOutput(true);
            hu.setDoInput(true);
            hu.setRequestMethod("POST");
            hu.setUseCaches(false);
            hu.setInstanceFollowRedirects(true);
            hu.setRequestProperty(" Content-Type ",
                    " application/x-www-form-urlencoded ");
            //hu.setChunkedStreamingMode(5);
            hu.connect();

            DataOutputStream out = new DataOutputStream(hu.getOutputStream());
            String content = key + cityName;
            //String content="key=4a417aba564b4f6a82ba71ab864a3035&cityname=%E6%88%90%E9%83%BD";
            out.writeBytes(content);
            out.flush();
            out.close();

            InputStream is = hu.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String str;
            while ((str = br.readLine()) != null) {
                System.out.println(str);
            }
            br.close();
            hu.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
